package com.aviccxzx.smarthome.UserModule;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.entity.FeedBack;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.gizwits.gizwifisdk.api.GizWifiSDK;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by joey on 2016/10/25.
 */
public class FeedBackActivity extends GosUserModuleBaseActivity implements View.OnClickListener {

    EditText content;//定义一个文本输入框
    TextView hasnum;// 用来显示剩余字数
    int num = 200;//限制的最大字数

    private Dialog dialog;
    String mContent = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.feedback_lay_v19);
        } else {
            setContentView(R.layout.feedback_lay);

        }
        ((TextView) findViewById(R.id.title_tv)).setText("意见反馈");
        setEdittext();
        findViewById(R.id.button_submit).setOnClickListener(this);
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
    }

    private void setEdittext() {
        content = (EditText) findViewById(R.id.et_content);

        hasnum = (TextView) findViewById(R.id.tv_num);
        hasnum.setText("200");
        content.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                temp = charSequence;
            }

            @Override
            public void afterTextChanged(Editable editable) {

                mContent = editable.toString();
                int number = num - editable.length();
                hasnum.setText("" + number);
                selectionStart = content.getSelectionStart();
                selectionEnd = content.getSelectionEnd();
                if (temp.length() > num) {
                    editable.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    content.setText(editable);
                    content.setSelection(tempSelection);//设置光标在最后
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_submit:
                if (!TextUtils.isEmpty(mContent)){
                    progressDialog.show();
                    commitText();
                }else {
                    showToast("请输入反馈内容");
                }
                break;
            case R.id.ll_title_left_view:
                finish();
                break;
        }
    }

    public void feedToGiz(){
        GizWifiSDK.sharedInstance().userFeedback("中航智能家居",mContent,true);
    }

    private void commitText() {
        String uid = spf.getString("Uid", "");
        String phone = spf.getString("UserName", "");
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<FeedBack> call = service.addFeedBack(uid, mContent, phone);
        call.enqueue(new Callback<FeedBack>() {
            @Override
            public void onResponse(Call<FeedBack> call, Response<FeedBack> response) {
                progressDialog.dismiss();
                App.getApp().showToast("意见反馈成功");
                finish();
            }

            @Override
            public void onFailure(Call<FeedBack> call, Throwable t) {
                progressDialog.dismiss();
                App.getApp().showToast("意见反馈失败"+"....."+t.getMessage());
            }
        });
    }
}
